package com.czy.owner.ui.workorder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import com.czy.owner.R;
import com.czy.owner.adapter.CommonTabLayoutAdapter;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.BaseFragment;
import com.czy.owner.utils.PhoneUtils;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

/**
 * Created by wangyang on 2016/7/7 0007.
 * 预约订单
 */
public class SubscribeOrdelActivity extends BaseActivity {
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    private List<String> titles;
    private CommonTabLayoutAdapter mFragmentAdapter;
    private List<BaseFragment> mFragments = new ArrayList<>();
    private static int currentId = 0;
    public static String orderAction = "order.action.reload.fragment";
    private BroadcastReceiver reloadReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(orderAction)) {
                currentId = intent.getIntExtra("currentId", 0);
                viewPager.setCurrentItem(currentId);
                mFragmentAdapter.reloadFragment(currentId);
            }
        }
    };

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_tablayout;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.work_order);
        currentId = getIntent().getIntExtra("currentId", 0);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(orderAction);
        registerReceiver(reloadReceiver, intentFilter);
        initViewPager();
    }

    @Override
    protected void updateViews() {

    }

    private void initViewPager() {
        addTabTitle();
        addTabFragment();
        mFragmentAdapter = new CommonTabLayoutAdapter(getSupportFragmentManager(), mFragments, titles);
//        //给ViewPager设置适配器
        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setOffscreenPageLimit(titles.size());
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(currentId);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentId = position;
                mFragmentAdapter.reloadFragment(position);
                currentId();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void addTabTitle() {
        titles = new ArrayList<>();
        titles.add("全部");
        titles.add("待处理");
        titles.add("待服务");
        titles.add("待付尾款");
        titles.add("已结束");
        for (int i = 0; i < titles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
        }
        if(PhoneUtils.getPhoneWidth(SubscribeOrdelActivity.this)>=1080){
            mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        }else {
            mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        }
    }

    public  static int currentId(){
      return currentId;
    }

    private void addTabFragment() {
        AllOrderFragment2 allOrderFragment = new AllOrderFragment2();
        DepositPaidFragment depoitPaidFragment = new DepositPaidFragment();
        ToBeServedFragment toBeServedFragment = new ToBeServedFragment();
        EndOfPaymentFragment endOfPaymentFragment = new EndOfPaymentFragment();
        HasEndedFragment hasEndedFragment = new HasEndedFragment();
        mFragments.add(allOrderFragment);
        mFragments.add(depoitPaidFragment);
        mFragments.add(toBeServedFragment);
        mFragments.add(endOfPaymentFragment);
        mFragments.add(hasEndedFragment);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(reloadReceiver);
    }
}
